package com.mj.activity.user;

import java.io.File;
import java.util.HashMap;
import java.util.Map;

import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.os.Environment;
import android.text.TextUtils;
import android.view.View;
import android.widget.DatePicker;
import android.widget.Toast;
import android.widget.DatePicker.OnDateChangedListener;

import com.mj.MJApplaction;
import com.mj.R;
import com.mj.activity.MainActivity;
import com.mj.activity.publish.PicActivity;
import com.mj.base.BaseActivity;
import com.mj.http.Url;
import com.mj.util.Constant;

public class Activity_Info extends BaseActivity {
	private String nickname;
	private int sex = 1;
	private String birthday;
	private File avatar = null;

	@Override
	protected void onCreate(Bundle arg0) {
		super.onCreate(arg0);
		setContentView(R.layout.activity_info);
		DatePicker datePicker = (DatePicker) findViewById(R.id.datePicker1);
		datePicker.init(2014, 8, 15, new OnDateChangedListener() {
			@Override
			public void onDateChanged(DatePicker arg0, int arg1, int arg2, int arg3) {
				birthday = arg1 + "-" + (arg2 + 1) + "-" + arg3;
			}
		});
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		if (resultCode != RESULT_OK) {
			finish();
			return;
		}
		switch (requestCode) {
		case Constant.PIC:
			showImg(data);
			break;
		case Constant.VIDEO:
			break;
		}
	}

	private void showImg(Intent data) {
		if (data == null) {
			return;
		}
		Bitmap bitmap = data.getExtras().getParcelable("data");
		avatar = new File(Environment.getExternalStorageDirectory() + "/temp_update.jpg");
		aq.id(R.id.imageView4).image(bitmap);
	}

	public void changeSex(View view) {
		String sexStr = view.getTag().toString();
		if (sexStr.equals("male")) {
			sex = 1;
			aq.id(R.id.imageView3).image(R.drawable.check_p);
			aq.id(R.id.imageView2).image(R.drawable.check_n);
		}
		if (sexStr.equals("female")) {
			sex = 2;
			aq.id(R.id.imageView3).image(R.drawable.check_n);
			aq.id(R.id.imageView2).image(R.drawable.check_p);
		}
	}

	public void pic(View view) {
		Intent intent = new Intent(this, PicActivity.class);
		startActivityForResult(intent, Constant.PIC);
	}

	public void commit(View view) {
		if (checkAll()) {
			baseCallBack callBack = new baseCallBack() {
				public void onSuccess(org.json.JSONObject object) {
					super.onSuccess(object);
					Intent intent = new Intent();
					intent.setClass(Activity_Info.this, MainActivity.class);
					startActivity(intent);
					finish();
				};
			};
			String url = Url.upinfo;
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("userkey", MJApplaction.userInfo.getUserkey());
			map.put("nickname", nickname);
			map.put("sex", sex);
			map.put("birthday", birthday);
			map.put("avatar", avatar);
			postJson(url, map, callBack, true);
		}
	}

	private boolean checkAll() {
		nickname = aq.id(R.id.editText1).getText().toString();
		if (TextUtils.isEmpty(nickname)) {
			Toast.makeText(this, "请输入昵称", Toast.LENGTH_LONG).show();
			aq.id(R.id.editText1).getView().requestFocus();
			return false;
		}
		if (TextUtils.isEmpty(birthday)) {
			Toast.makeText(this, "请选择生日", Toast.LENGTH_LONG).show();
			return false;
		}
		if (avatar == null) {
			Toast.makeText(this, "请选择头像", Toast.LENGTH_LONG).show();
			return false;
		}
		return true;
	}
}
